package com.cocotingo.snail.template.models;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.cocotingo.snail.template.TemplateSyntaxException;

public class VariablesNode extends Node {
	
	private final List<VariableNode<?>> variables;
	
	public VariablesNode() {
		variables = new ArrayList<VariableNode<?>>();
	}
	
	public final void addVariableNode(VariableNode<?> node) {
		this.addVariableNode(node, variables.size());
	}
	
	public void addVariableNode(VariableNode<?> node, int index) {
		if (node == null) throw new IllegalArgumentException("cannot add null as a child node");
		variables.add(index, node);
	}
	
	public boolean removeVariableNode(VariableNode<?> node) {
		return variables.remove(node);
	}
	
	public Iterable<VariableNode<?>> variableNodes() {
		return new Iterable<VariableNode<?>>() {
			@Override
			public Iterator<VariableNode<?>> iterator() {
				return variables.iterator();
			}
		};
	}

	@Override
	public void validateNode() throws TemplateSyntaxException {
	}

	@Override
	public Iterable<Node> children() {
		return new Iterable<Node>() {
			@Override
			public Iterator<Node> iterator() {
				return new Iterator<Node>() {
					Iterator<VariableNode<?>> it = variables.iterator();
					@Override
					public boolean hasNext() {
						return it.hasNext();
					}
					@Override
					public Node next() {
						return it.next();
					}
					@Override
					public void remove() {
						throw new UnsupportedOperationException();
					}
				};
			}
		};
	}

	@Override
	protected String getXMLNodeName() {
		return "Variables";
	}

	@Override
	protected String getXMLAttributes() {
		return null;
	}

}
